package com.example.himalaya2.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya2.R;
import com.example.himalaya2.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class RecommendListAdapter extends RecyclerView.Adapter<RecommendListAdapter.InnerHolder> {
    private static final String TAG = "RecommendListAdapter";

    private List<Album> mData = new ArrayList<>();
    private OnRecommendItemClickListener mItemClickListener = null;

    @NonNull
    @NotNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        // 找到子项的view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecommendListAdapter.InnerHolder holder, int position) {
        // 封装数据
        holder.itemView.setTag(position);  // 每一个item都要经过这里
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(position,mData.get(position));
                }
                LogUtil.d(TAG,"item view clicked item position:" + position);
            }
        });
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {  // 返回要显示的个数
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public void setData(List<Album> albumList) {
        if (mData != null) {  // 数据不为空，清掉上次的数据
            mData.clear();
            mData.addAll(albumList);
        }
        // 更新ui
        notifyDataSetChanged();
    }

    public int getDataSize() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }

        public void setData(Album album) {
            // 找到各个控件 并设置数据
            // 专辑封面
            ImageView albumCoverIv = itemView.findViewById(R.id.album_cover);
            // title
            TextView albumTitleTv = itemView.findViewById(R.id.album_title_tv);
            // 描述
            TextView albunmDescrTV = itemView.findViewById(R.id.album_description_tv);
            // 播放的数量
            TextView albumPlayCountTv = itemView.findViewById(R.id.album_play_count);
            // 集数
            TextView albumContentCountTv = itemView.findViewById(R.id.album_content_size);

            albumTitleTv.setText(album.getAlbumTitle());
            albunmDescrTV.setText(album.getAlbumIntro());
            albumPlayCountTv.setText((((int)album.getPlayCount())/10000)+ itemView.getContext().getString(R.string.playcountunit));
            albumContentCountTv.setText(album.getIncludeTrackCount()+"");
            final String coverUrlLarge = album.getCoverUrlLarge();
            if (!TextUtils.isEmpty(coverUrlLarge)) {
                Picasso.with(itemView.getContext()).load(coverUrlLarge).into(albumCoverIv);
            } else {
                albumCoverIv.setImageResource(R.mipmap.ximalay_logo);
            }
        }
    }
    public void setOnRecommendItemClickListener(OnRecommendItemClickListener listener){
        this.mItemClickListener = listener;
    }
    public interface OnRecommendItemClickListener{
        void onItemClick(int position,Album album);
    }
}
